package com.jack.thinking.java._22.swing.text;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

import com.utils.SwingConsole;

public class TextFields extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton b1 = new JButton("Get Text"),
					b2 = new JButton("Set Text");
	
	private JTextField t1 = new JTextField(30),
					   t2 = new JTextField(30),
					   t3 = new JTextField(30);
	
	private String s = "";
	private UpperCaseDocument ucd = new UpperCaseDocument();
	
	public TextFields() {
		t1.setDocument(ucd);
		ucd.addDocumentListener(new T1());
		b1.addActionListener(new B1());
		b2.addActionListener(new B2());
		t1.addActionListener(new T1A());
		setLayout(new FlowLayout());
		add(b1);
		add(b2);
		
		add(t1);
		add(t2);
		add(t3);
	}
	
	/**
	 * 文档监听器: t2 和 t3 显示 t1的内容。
	 * 
	 * @author wangbingwu
	 *
	 */
	class T1 implements DocumentListener{

		@Override
		public void insertUpdate(DocumentEvent e) {
			t2.setText(t1.getText());
			t3.setText("Text: " + t1.getText());
		}

		@Override
		public void removeUpdate(DocumentEvent e) {
			t2.setText(t1.getText());
		}

		@Override
		public void changedUpdate(DocumentEvent e) {
			
		}
		
	}
	
	/**
	 * 动作监听器， t1 显示动作次数, 区别与DocumentListener 只有确定键 才会触发。
	 * @author wangbingwu
	 *
	 */
	class T1A implements ActionListener{
		private int count  = 0 ;

		@Override
		public void actionPerformed(ActionEvent e) {
			t3.setText("t1 action Event " + count++);
		}
		
	}
	
	/**
	 * 动作监听器： 获取t1 的已选择的文本
	 * @author wangbingwu
	 *
	 */
	class B1 implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(t1.getSelectedText() == null) {
				s = t1.getText();
			}else {
				s = t1.getSelectedText();
			}
			t1.setEditable(true);
		}
	}
	
	/**
	 * 动作监听器： 给t1 设置值，同时 修改UpperCaseDocument 中的状态位 
	 * @author wangbingwu
	 *
	 */
	class B2 implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			ucd.setUpperCase(false);
			t1.setText("Inserted by Button 2: " + s);
			ucd.setUpperCase(true);
			t1.setEnabled(false);
		}
		
	}
	
	class UpperCaseDocument extends PlainDocument{
		private static final long serialVersionUID = 1L;
		
		private boolean upperCase = true;
		
		public void setUpperCase(boolean flag) {
			upperCase = flag;
		}
		
		public void insertString(int offset, String str, AttributeSet attset) throws BadLocationException {
			if(upperCase) {
				str = str.toUpperCase();
			}
			super.insertString(offset, str, attset);
		}
	}
	
	public static void main(String[] args) {
		SwingConsole.run(new TextFields(), 375, 200);
	}
}
